/*
 * Created by JFormDesigner on Fri Jun 28 16:24:14 CST 2024
 */

package scct.yangxianpeng.ui;

import scct.yangxianpeng.dao.TypeDao;
import scct.yangxianpeng.model.GoodsType;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 35403
 */
public class DlgTypeUpdate extends JDialog {
    int id;

    public DlgTypeUpdate(Window owner, int id) {
        super(owner);
        this.id = id;
        initComponents();
        initView();
    }

    //        根据id查询用户，把用户添加到对话框的显示,把原数据显示在修改框中
    private void initView() {
        TypeDao typeDao = new TypeDao();
        GoodsType goodsType = typeDao.goodsTypeBtId(id);
        txtName.setText(goodsType.getName());
        txtRemark.setText(goodsType.getRemark());
    }

    //    取消按钮
    private void btnCancel(ActionEvent e) {
        this.dispose();
    }

    private void btnUpdate(ActionEvent e) {
        String name = txtName.getText();
        String remark = txtRemark.getText();
        if (name.length() == 0) {
            JOptionPane.showMessageDialog(this, "请输入合法的商品类别名称！");
            return;
        }
        if (remark.length() == 0) {
            JOptionPane.showMessageDialog(this, "请输入合法的商品类别说明！");
            return;
        }
        TypeDao typeDao = new TypeDao();
        if (typeDao.updateType(id, name, remark)) {
            JOptionPane.showMessageDialog(this, "修改成功！");
            dispose();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yang
        label1 = new JLabel();
        label2 = new JLabel();
        txtName = new JTextField();
        scrollPane1 = new JScrollPane();
        txtRemark = new JTextArea();
        btnUpdate = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u5546\u54c1\u7c7b\u522b");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4fee\u6539\u540e\u540d\u79f0\uff1a");
        contentPane.add(label1);
        label1.setBounds(65, 45, 88, label1.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u4fee\u6539\u540e\u8bf4\u660e\uff1a");
        contentPane.add(label2);
        label2.setBounds(65, 100, 88, label2.getPreferredSize().height);
        contentPane.add(txtName);
        txtName.setBounds(165, 30, 200, 45);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(txtRemark);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(165, 100, 200, 130);

        //---- btnUpdate ----
        btnUpdate.setText("\u4fee\u6539\u7c7b\u522b");
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(145, 255), btnUpdate.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        btnCancel.addActionListener(e -> btnCancel(e));
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(300, 255), btnCancel.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(500, 375);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yang
    private JLabel label1;
    private JLabel label2;
    private JTextField txtName;
    private JScrollPane scrollPane1;
    private JTextArea txtRemark;
    private JButton btnUpdate;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
